package com.zw.pdm.module.craft.controller;

import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.entity.com.DsnTaskCommon;
import com.zw.pdm.entity.craft.CraGant;
import com.zw.pdm.entity.craft.CraMakeDetail;
import com.zw.pdm.entity.craft.CraMakeSheet;
import com.zw.pdm.entity.craft.CraTaskDetail;
import com.zw.pdm.entity.designtask.DsnGant;
import com.zw.pdm.entity.designtask.DsnProStruct;
import com.zw.pdm.entity.est.Card;
import com.zw.pdm.entity.production.MakeResult;
import com.zw.pdm.entity.rep.Result;
import com.zw.pdm.entity.rep.Sheet;
import com.zw.pdm.entity.sys.Role;
import com.zw.pdm.module.basic.service.DictCommonService;
import com.zw.pdm.module.craft.mapper.CraftProQueryMapper;
import com.zw.pdm.module.craft.model.CraGantBillModel;
import com.zw.pdm.module.craft.model.CraProQueryModel;
import com.zw.pdm.module.craft.model.CraQueryModel;
import com.zw.pdm.module.craft.model.TaskStatusModel;
import com.zw.pdm.module.craft.service.CraftMaintainService;
import com.zw.pdm.module.craft.service.CraftProQueryService;
import com.zw.pdm.module.craft.service.CraftReceiveService;
import com.zw.pdm.module.craft.service.CraftReleaseService;
import com.zw.pdm.module.designtask.mapper.DsnGantMapper;
import com.zw.pdm.module.designtask.mapper.DsnProcessMapper;
import com.zw.pdm.module.designtask.model.DsnGantBillModel;
import com.zw.pdm.module.report.mapper.SheetMapper;
import com.zw.pdm.module.report.model.Project;
import com.zw.pdm.util.ShiroUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import static com.zw.pdm.constant.Constant.GROUP_MALTYPE;
import static com.zw.pdm.constant.Constant.GROUP_SPECTYPE;

/**
 * @Description: 计划查询接口
 * @Author: China.sgl
 * @Date: 2020/8/3 16:11
 */
@Controller
@RequestMapping("/craft/proQuery")
@SuppressWarnings("all")
public class CraftPeoQueryController extends BaseController {

    @Autowired
    private CraftReceiveService craftReceiveService;
    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private CraftReleaseService craftReleaseService;
    @Autowired
    private CraftMaintainService craftMaintainService;
    @Autowired
    private CraftProQueryService craftProQueryService;
    @Autowired
    private CraftProQueryMapper craftProQueryMapper;
    @Autowired
    private SheetMapper sheetMapper;
    @Autowired
    private DsnProcessMapper dsnProcessMapper;
    @Autowired
    private DsnGantMapper dsnGantMapper;

    @GetMapping
    @RequiresPermissions({"tec:proGress:view"})
    public String page(Model model) {
        model.addAttribute("specialId", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        model.addAttribute("deptName", craftReleaseService.findCraftSectionByDeptId(4));
        return "craft/proQuery/proQuery";
    }

    /**
     * 接收列表（原用 task表的数据）
     */
    @ResponseBody
    @PostMapping("/list")
    public JsonResult list(CraProQueryModel craProQueryModel) {
        startPage();
        List<CraProQueryModel> list = craftProQueryService.selectPro(craProQueryModel);
        return jsonResult(list);
    }
    /**
     * 接收列表改（用主表 反推项目表  按项目 查看多项目看板）
     */
    @ResponseBody
    @PostMapping("/listP")
    public JsonResult listP(CraQueryModel craQueryModel) {
        startPage();
        List<CraQueryModel> list = craftProQueryService.selectProDetail(craQueryModel);
        return jsonResult(list);
    }
    /**
     * 接收列表改（用主表 反推项目表  按部分 查看多项目看板）
     */
    @ResponseBody
    @PostMapping("/listPart")
    public JsonResult listPart(CraQueryModel craQueryModel) {
        startPage();
        List<CraQueryModel> list = craftProQueryService.selectProDetailByPart(craQueryModel);
        return jsonResult(list);
    }
    /**
     * 查看操作
     */
    @GetMapping("/info/{proId}")
    public String info(@PathVariable("proId") Integer proId, Model model) {
        CraProQueryModel craProQueryModel = craftProQueryService.selectProqueryById(proId);
        model.addAttribute("craProQueryModel", craProQueryModel);
        CraProQueryModel craProQueryModel1 = new CraProQueryModel();
        craProQueryModel1.setProId(proId);
        List<CraProQueryModel> craProQueryModelList = craftProQueryService.selectPro(craProQueryModel1);
        model.addAttribute("craProQueryModelList", craProQueryModelList);
        return "craft/proQuery/detailInfo";
    }
    /**
     * 查看操作(按项目分多项目看板)
     */
    @GetMapping("/infoP/{proId}")
    public String infoP(@PathVariable("proId") Integer proId, Model model) {
        CraProQueryModel craProQueryModel = craftProQueryService.selectProqueryById(proId);
        model.addAttribute("craProQueryModel", craProQueryModel);
        CraProQueryModel craProQueryModel1 = new CraProQueryModel();
        craProQueryModel1.setProId(proId);
        List<CraProQueryModel> craProQueryModelList = craftProQueryService.selectPro(craProQueryModel1);
        model.addAttribute("craProQueryModelList", craProQueryModelList);
        return "craft/proQuery/info";
    }
    /**
     * 查看操作(按部分分多项目看板)
     */
    @GetMapping("/infoPart/{proId}")
    public String infoPart(@PathVariable("proId") Integer proId, Model model) {
        CraProQueryModel craProQueryModel = craftProQueryService.selectProqueryById(proId);
        model.addAttribute("craProQueryModel", craProQueryModel);
        CraProQueryModel craProQueryModel1 = new CraProQueryModel();
        craProQueryModel1.setProId(proId);
        List<CraProQueryModel> craProQueryModelList = craftProQueryService.selectPro(craProQueryModel1);
        model.addAttribute("craProQueryModelList", craProQueryModelList);
        String setionNames = craftProQueryService.selectCraMakeSheetSetionNameByProId(proId);
        model.addAttribute("setionNames", setionNames);
        return "craft/proQuery/info";
    }
    /**
     * 工艺编制
     */
    @ResponseBody
    @PostMapping("/detail/craft")
    public JsonResult card(CraMakeSheet craMakeSheet, Model model) {
        List<CraMakeSheet> craMakeSheetList = craftProQueryService.selectCraCardSheetByD(craMakeSheet);
        return jsonResult(craMakeSheetList);
    }

    /**
     * 原材料
     */
    @ResponseBody
    @PostMapping("/detail/material")
    public JsonResult outsourcing(CraMakeSheet craMakeSheet, Model model) {
        Sheet sheet = new Sheet();
        sheet.setProCode(craMakeSheet.getProCode());
        sheet.setPart(craMakeSheet.getPart());
        sheet.setMalType(craMakeSheet.getSpecial());
        sheet.setReportType(2);
        List<Sheet> list = craftProQueryService.selectRepResultBysheetId(sheet);
        return jsonResult(list);
    }

    /**
     * 外协件
     */
    @ResponseBody
    @PostMapping("/detail/outsourcing")
    public JsonResult material(CraMakeSheet craMakeSheet, Model model) {
        Sheet sheet = new Sheet();
        sheet.setProCode(craMakeSheet.getProCode());
        sheet.setPart(craMakeSheet.getPart());
        sheet.setMalType(craMakeSheet.getSpecial());
        sheet.setReportType(3);
        List<Sheet> list = craftProQueryService.selectRepResultBysheetId(sheet);
        return jsonResult(list);
    }

    /**
     * 任务节点编辑
     */
    @ResponseBody
    @PostMapping("/detail/status")
    @RequiresPermissions({"tec:proSproject:edit"})
    public JsonResult statusChange(Integer deptId, Integer statusSj, Integer statusSC, String code, Model model) {
        startPage();
        return jsonResult(craftProQueryService.updateTastCommonStatus(deptId, statusSj, statusSC, code));
    }

    /**
     * 根据条件查询部分
     *
     * @param proId
     * @return
     */
    @ResponseBody
    @GetMapping("/proStruct/{proId}")
    public List<DsnProStruct> findProStructByProId(@PathVariable("proId") Integer proId) {
        return craftProQueryService.findProStructByProId(proId);
    }

    /**
     * 查询明细-单项目看板
     * @param proCode
     * @param model
     * @return
     */
    @GetMapping("/outPage")
    public String outPage(String proCode,Integer reportType, Model model) {
        model.addAttribute("malTypes", dictCommonService.findDictByGroupName(GROUP_MALTYPE));
        Project project = sheetMapper.findProjectByCode(proCode).get(0);
        model.addAttribute("proCode", project.getCode());
        model.addAttribute("proName", project.getName());
        model.addAttribute("proNum", project.getNum());
        model.addAttribute("reportType", reportType);
        if (reportType==2){
            model.addAttribute("reportTypeName", "外协件提报计划");
        }
        if (reportType==3){
            model.addAttribute("reportTypeName", "原材料提报计划");
        }
        return "craft/proQuery/makeDetailPage";
    }
    /**
     * 提报计划输出表
     *
     * @param result
     * @return
     */
    @ResponseBody
    @PostMapping("/makeResultList")
    public JsonResult makePage(Result result) {
        startPage();
        List<Result> list = craftProQueryService.findResultByQuery(result);
        return jsonResult(list);
    }
    /**
     * 项目任务进度列表
     */
    @ResponseBody
    @PostMapping("/taskStatus")
//    @RequiresPermissions({"report:query:view"})
    public JsonResult taskStatus(Integer proId) {
        List<CraTaskDetail> list = craftProQueryService.selectTaskStatusModel(proId);
        return jsonResult(list);
    }
    /**
     * 查询成本信息输出 明细 （根据项目号 和 任务名称Id 查询）
     */
    @ResponseBody
    @PostMapping("/costStatus")
//    @RequiresPermissions({"report:query:view"})
    public JsonResult costStatus(Integer proId,Integer comTaskNameId) {
        DsnTaskCommon dsnTaskCommon = new DsnTaskCommon();
        dsnTaskCommon.setProId(proId);
        dsnTaskCommon.setComTaskNameId(comTaskNameId);
        List<DsnTaskCommon> list = craftProQueryService.selectdsnTaskCommonByProIdAndComTaskNameId(dsnTaskCommon);
        return jsonResult(list);
    }

    /**
     * 保存成本信息输出模块
     * @param dsnTaskCommon
     * @return
     */
    @ResponseBody
    @PostMapping("/costSave")
    public  JsonResult costSave(DsnTaskCommon dsnTaskCommon){

        return  jsonResult(craftProQueryService.updateCostStatus(dsnTaskCommon));
    }
    /**
     * 查询成本信息输出 明细 （根据项目号 和 任务名称Id 查询）
     */
    @ResponseBody
    @PostMapping("/nodeStatus")
//    @RequiresPermissions({"report:query:view"})
    public JsonResult nodeStatus(Integer proId,Integer specialId,Integer comTaskNameId) {
        DsnTaskCommon dsnTaskCommon = new DsnTaskCommon();
        dsnTaskCommon.setProId(proId);
        dsnTaskCommon.setComTaskNameId(comTaskNameId);
        dsnTaskCommon.setSpecialId(specialId);
        List<DsnTaskCommon> list = craftProQueryService.selectdsnTaskCommonByProIdAndComTaskNameId(dsnTaskCommon);
        return jsonResult(list);
    }
    /**
     * 查询成本信息输出 明细 （根据项目号 和 任务名称Id 查询）
     */
    @ResponseBody
    @PostMapping("/partList")
//    @RequiresPermissions({"report:query:view"})
    public JsonResult partList(Integer proId,Integer specialId,Integer comTaskNameId) {
        DsnTaskCommon dsnTaskCommon = new DsnTaskCommon();
        dsnTaskCommon.setProId(proId);
        dsnTaskCommon.setComTaskNameId(comTaskNameId);
        dsnTaskCommon.setSpecialId(specialId);
        List<DsnTaskCommon> list = craftProQueryService.selectdsnTaskCommonByProIdAndComTaskNameId(dsnTaskCommon);
        return jsonResult(list);
    }
    /**
     * 查询成本信息输出 明细 （根据项目号 和 任务名称Id 查询）
     */
    @ResponseBody
    @PostMapping("/editTaskStatus")
//    @RequiresPermissions({"report:query:view"})
    public JsonResult editTaskStatus(DsnTaskCommon dsnTaskCommon) {

        return jsonResult(craftProQueryService.editTaskStatus(dsnTaskCommon));
    }
    /** 跳转任务看板*/
    @GetMapping("/tzrwList")
//    @RequiresPermissions({"dsn:proBull:view"})
    public String tzrwList(Model model,String proCode) {
        Project project = sheetMapper.findProjectByCode(proCode).get(0);
        model.addAttribute("proId", project.getId());
        model.addAttribute("proCode", proCode);
        model.addAttribute("proName", project.getName());
        model.addAttribute("deptId", ShiroUtils.getUser().getDeptId());
        model.addAttribute("deptName", ShiroUtils.getUser().getBranchName());
        model.addAttribute("createName", ShiroUtils.getUser().getName());
        List<Role> roleList = craftProQueryService.findUserRoleByUserId(ShiroUtils.getUserId());
        String url = "";
        if (roleList != null && roleList.size() > 0){
            url = "craft/proQuery/createGantEditPage";
        }else {
            url = "craft/proQuery/gantPage";
        }
        return url;
    }
    /**
     * 根据项目编号和登入人员获取项目计划
     *
     * @param proCode 项目编号
     * @return
     */
    @GetMapping("/gantList")
    @ResponseBody
//    @RequiresPermissions({"dsn:proTasks:view"})
    public JsonResult gantList(String proCode) {

        return jsonResult(craftProQueryService.findGantListByProCodeAndCreateCode(proCode, ShiroUtils.getUser().getAccount()));
    }
    /**
     * 保存项目计划甘特图
     *
     * @param mOrderInsertModel
     * @return
     */
    @ResponseBody
    @PostMapping("/save")
//    @RequiresPermissions({"dsn:proTasks:edit"})
    public JsonResult outCreate(@RequestBody CraGantBillModel craGantBillModel) {
        return jsonResult(craftProQueryService.save(craGantBillModel));
    }
    /**
     * 项目任务项查看
     *
     * @param dsnGant
     * @return
     */
    @ResponseBody
    @PostMapping("/gantPage")
//    @RequiresPermissions({"dsn:proTaskinfor:view"})
    public JsonResult findDsnGantByQuery(CraGant craGant) {
        if (craGant.getProCodeQuery() != null && craGant.getProCodeQuery() != ""){
            List<Integer> ids = dsnGantMapper.findDeptIdsByDeptId(ShiroUtils.getUser().getDeptId());
            // 没有符合要求的科室带入一个查询不到的条件，否则会查询全部记录
            if (ids.size() > 0) {
                craGant.setIds(StringUtils.join(ids.toArray(), ","));
            }
        }
        startPage();
        List<CraGant> list = craftProQueryService.findDsnGantByQuery(craGant);
        return jsonResult(list);
    }
}
